import { DoWebSock } from "./doWebSock";
import { DoHttp, IGameHttpRequest } from "./doHttp";
import { COMMON_PB_COMMAND } from "../proto/proto";

export class DoNetwork {
    private webSock: DoWebSock = null;
    private _startReconnHandle: () => void = null;
    private _reconnectedHandle: () => void = null;

    private is_schedule_open: boolean = false;

    constructor(startReconnHandle: () => void, reconnectedHandle: () => void) {
        this.webSock = new DoWebSock(this.onWebSockReconn.bind(this));
        this._startReconnHandle = startReconnHandle;
        this._reconnectedHandle = reconnectedHandle;
        this.is_schedule_open = false;

    }

    // 请求连接时调用！！！
    public ws_connect(url: string, 
        succHanle: () => void, 
        failHandle: () => void) {
        var self = this;
        if (!url) {
            console.log("DoNetwork ws_connect 异常！！！ url :" + url);
            return;
        }

        // 成功的回调
        var successful = function () {
            console.log("DoNetwork ws_connect successful");
            succHanle();
            // 心跳部分往上移
            // self.start_heart_beat();
            // self._pingpong();
        }

        // 完善一下url之后连接
        // var ws_url = game_connect_type + "://" + url + "/websocket";
        var ws_url = url + "/websocket";
        console.log("network @@@@ ws_connect @@@@ ws_url :" + ws_url + "\n\n\n");
        this.webSock.connect(ws_url, successful, failHandle);

    }

    // webSock重连执行
    public onWebSockReconn() {
        var self = this;
        console.log("DoNetwork onWebSockReconn 重连！！！ ");

        // 心跳部分往上移
        // self.stop_heart_beat();
        this._startReconnHandle();

        var link_successful = function () {
            console.log("DoNetwork @@@@@@@@@@@@ on_ws_reconnect @@@@ link_successful ");

            self._reconnectedHandle();
            // 心跳部分往上移
            // self.start_heart_beat();

            // 通知重连成功！！！
            // g_notifyer.notify_ui_event("core_notify_ws_reconnect_success");

            // 如果重连成功，且在房间内！！！这部分移到上层去
            // if (window.login_inTable == true) {
            //     self.on_ws_reconnect_in_table();
            // }
        }

        var link_failed = function () {
            console.log("DoNetwork @@@@@@@@@@@@ on_ws_reconnect @@@@ link_failed ");
        }
        var url = this.webSock.getUrl();
        console.log("DoNetwork @@@@@@@@@@@@ on_ws_reconnect @@@@ center_server_url :" + url);
        // this.wbsocket.connect(game_connect_type + "://" + game_server_addr + "/websocket", link_successful, link_failed)
        this.ws_connect(url, link_successful, link_failed);
    }

    public cancelWebSockConfirm() {
        this.webSock.onCancel();
    }
    // public onWebSockConfirm() {
    //     // token为空，等待登录中，不再自动重连
    //     if (window["account_token"] == undefined || window["account_token"] === "") {
    //         console.log("DoNetwork onWebSockConfirm token为空，等待登录中，不再自动重连");
    //         this.webSock.onCancel();
    //         return;
    //     }    

    //     // 认证ws
    //     var data : IRequestProto = {
    //         cmd: COMMON_PB_COMMAND.MSG_CONFIRM_REQUEST,
    //     }
    //     data.confirmRequest = {
    //         // token: globalThis.account_token
    //         token: window["account_token"]
    //     }
    //     this.wsRequestPb(data);
    // }

    public wsRequestPb(msg: IRequestProto) {

        if (this.is_connect() == false) {
            // todo 为啥注释
            // console.log("DoNetwork wsRequestPb 连接已断开，等待重新连接 ");

            // 通知等待重连！！！
            // g_notifyer.notify_ui_event("core_notify_ws_reconnect_need");
            return;
        }

        // console.log("DoNetwork @@@@@@@@@@@@ wsRequestPb @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ↓↓↓↓↓↓↓↓↓↓ ");
        console.log("DoNetwork wsRequestPb 【发送】ws_msg : " + JSON.stringify(msg));

        // 具体的 pb Request 编码
        var bodyData = RequestProto.encode(msg).finish();
        // cc.log("DoNetwork @@@@@@@@@@@@ wsRequestPb @@@@ bodyData :" + JSON.stringify(bodyData));

        // 重新组合GameMessage
        var messageData : IGameMessage = {
            messageId: msg.cmd,
            body: bodyData,
        };

        // cc.log("DoNetwork @@@@@@@@@@@@ wsRequestPb @@@@ messageData :" + JSON.stringify(messageData));

        // pb GameMessage 编码
        var requestPb = GameMessage.encode(messageData).finish();
        // cc.log("DoNetwork @@@@@@@@@@@@ wsRequestPb @@@@ requestPb :" + JSON.stringify(requestPb));

        this.webSock.send_pb(requestPb);
        // cc.log("DoNetwork @@@@@@@@@@@@ wsRequestPb @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ↑↑↑↑↑↑↑↑↑↑\n\n\n");
    }

    public restartWebSockReconn() {
        console.log("DoNetwork restartWebSockReconn 立即！！重连 ");
        this.webSock.setAllowReconn(true);
        this.webSock.resetReconnCount();
        this.webSock.startReconnect();
    }

    public closeWebSock() {
        this.webSock.ws_clear_interface();
    }

    public is_connect(){
        return this.webSock.isLink();
    }

    public httpRequest(request: IGameHttpRequest, failHandle: (status: number, eventName: string) => void, succHandle: (data: any,eventName: string) => void) {
        var http = new DoHttp(request, failHandle, succHandle);
        http.request();
    }
}

